home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 May: Tool Chest / Dev.CD May 00 TC.toast / pc / what's new? / sample code / processes / mpintertasktime / mpintertasktime.c next >
Encoding:
Text File  |  2000-03-15  |  8.3 KB  |  291 lines

  1. /*
  2.     File:        "MPPeriodicalTest.c"
  3.     
  4.     Description:
  5.                 This is a application to measure the intertask signaling time.
  6.  
  7.     Version:    v0.0
  8.  
  9.     File Ownership:
  10.  
  11.         DRI:                George Warner
  12.  
  13.         Other Contact:        
  14.  
  15.         Technology:            MultiTasking/MultiProcessing
  16.  
  17.     Copyright:     © Copyright 2000 Apple Computer, Inc. All rights reserved.
  18.     
  19.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  20.                 ("Apple") in consideration of your agreement to the following terms, and your
  21.                 use, installation, modification or redistribution of this Apple software
  22.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  23.                 please do not use, install, modify or redistribute this Apple software.
  24.  
  25.                 In consideration of your agreement to abide by the following terms, and subject
  26.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  27.                 copyrights in this original Apple software (the "Apple Software"), to use,
  28.                 reproduce, modify and redistribute the Apple Software, with or without
  29.                 modifications, in source and/or binary forms; provided that if you redistribute
  30.                 the Apple Software in its entirety and without modifications, you must retain
  31.                 this notice and the following text and disclaimers in all such redistributions of
  32.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  33.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  34.                 Apple Software without specific prior written permission from Apple.  Except as
  35.                 expressly stated in this notice, no other rights or licenses, express or implied,
  36.                 are granted by Apple herein, including but not limited to any patent rights that
  37.                 may be infringed by your derivative works or by other works in which the Apple
  38.                 Software may be incorporated.
  39.  
  40.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  41.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  42.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  44.                 COMBINATION WITH YOUR PRODUCTS.
  45.  
  46.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  47.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  48.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  49.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  50.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  51.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  52.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  53.  
  54.     Writers:
  55.  
  56.         gaw    - George Warner
  57.  
  58.     Change History (most recent first):
  59.  
  60.          <1>    02/29/00    gaw     Initial build
  61.  
  62.     Author Initials:
  63.  
  64.     gaw - George Warner
  65.  
  66. */
  67. //**************************************************************************
  68. #pragma mark compiler directives
  69.  
  70. //**************************************************************************
  71. #pragma mark #Includes
  72. #include <MacTypes.h>
  73. #include <math64.h>
  74. #include <Multiprocessing.h>
  75. //#include <DriverServices.h>
  76.  
  77. #include <stdio.h>            // for printf & fflush
  78. #include <SIOUX.h>            // for SIOUXSettings stuff
  79. #include <SIOUXGlobals.h>    // for SIOUXQuitting
  80.  
  81. //**************************************************************************
  82. #pragma mark typedefs, structs, enums, defines, etc.
  83. enum {
  84.     aQueue = 0,
  85.     aSemaphore,
  86.     anEvent
  87. }gReflectOP;
  88.  
  89. //**************************************************************************
  90. #pragma mark static (local) globals
  91. static MPOpaqueID gWaiterID,gPostID;
  92.  
  93. //**************************************************************************
  94. #pragma mark static (local) functions
  95. static OSStatus ReflectorTask(void*)
  96. {
  97.     void    *p1,*p2,*p3;
  98.     MPEventFlags events;
  99.  
  100.     while (true)
  101.     {
  102.         switch (gReflectOP)
  103.         {
  104.         case aQueue:
  105.             MPWaitOnQueue((MPQueueID) gWaiterID,&p1,&p2,&p3,kDurationForever);
  106.             break;
  107.         case aSemaphore:
  108.             MPWaitOnSemaphore((MPSemaphoreID) gWaiterID,kDurationForever);
  109.             break;
  110.         case anEvent:
  111.             MPWaitForEvent((MPEventID) gWaiterID,&events,kDurationForever);
  112.             break;
  113.         default:
  114.             return -123;
  115.         }
  116.         switch (gReflectOP)
  117.         {
  118.             case aQueue:
  119.                 MPNotifyQueue((MPQueueID) gPostID,&p1,&p2,&p3);
  120.                 break;
  121.             case aSemaphore:
  122.                 MPSignalSemaphore((MPSemaphoreID) gPostID);
  123.                 break;
  124.             case anEvent:
  125.                 MPSetEvent((MPEventID) gPostID,0x01010101);
  126.                 break;
  127.             default:
  128.                 return -123;
  129.         }
  130.     }
  131.     return -123;
  132. }
  133.  
  134. static float HowLong(AbsoluteTime pEndTime,AbsoluteTime pBgnTime)
  135. {
  136.     AbsoluteTime absTime;
  137.     Nanoseconds nanosec;
  138.  
  139.     absTime = SubAbsoluteFromAbsolute(pEndTime,pBgnTime);
  140.     nanosec = AbsoluteToNanoseconds(absTime);
  141.     return (float) (UnsignedWideToUInt64(nanosec) / 1000.0f);
  142. }
  143.  
  144. void main (void)
  145. {
  146.     OSStatus anErr;
  147.     MPTaskID task;
  148.     UInt32 i,count;
  149.     void *p1,*p2,*p3;
  150.     MPEventFlags events;
  151.     AbsoluteTime nowTime,bgnTime;
  152.     float uSec;
  153.  
  154.     // Set the SIOUX window defaults
  155.     SIOUXSettings.autocloseonquit    = false;
  156.     SIOUXSettings.asktosaveonclose    = false;
  157.     SIOUXSettings.showstatusline    = false;
  158.     SIOUXSettings.columns            = 132;
  159.     SIOUXSettings.rows                = 24;
  160.     SIOUXSettings.fontsize            = 10;
  161.     GetFNum("\pMonaco",&SIOUXSettings.fontid);
  162.     SIOUXSettings.standalone        = true;
  163.  
  164.     // Make sure that the MP library is loaded
  165.     if (!MPLibraryIsLoaded())
  166.     {
  167.         printf("The MP library did not load.\n");
  168.         return;
  169.     }
  170.  
  171.     // Find the overhead up UpTime.Perform a bunch 
  172.     // of calls to average out cache effects.
  173.  
  174.     printf("\n");
  175.  
  176.     bgnTime = UpTime();
  177.     for (i= 0;i<16;i++)
  178.         nowTime = UpTime();
  179.  
  180.     uSec = HowLong(nowTime,bgnTime) / 16.0f;
  181.     printf("UpTime overhead:%.3f usec \n",uSec);
  182.  
  183.     // Time intertask communication.
  184.     printf("\n Queues \n");
  185.     gReflectOP = aQueue;
  186.     MPCreateQueue((MPQueueID*) &gWaiterID);
  187.     MPCreateQueue((MPQueueID*) &gPostID);
  188.  
  189.     bgnTime = UpTime();
  190.     anErr = MPCreateTask(ReflectorTask,NULL,0,NULL,0,0,kNilOptions,&task);
  191.     nowTime = UpTime();
  192.  
  193.     uSec = HowLong(nowTime,bgnTime);
  194.     printf("MPCreateTask overhead:%.3f usec (may vary significantly)\n",uSec);
  195.     if (noErr != anErr)
  196.     {
  197.         printf("Task not created!\n");
  198.         return;
  199.     }
  200.  
  201.     count = 100000;
  202.     bgnTime = UpTime();
  203.     for (i= 0;i<count;i++)
  204.     {
  205.         MPNotifyQueue((MPQueueID)gWaiterID,0,0,0);
  206.         while (true)
  207.         {
  208.             anErr = MPWaitOnQueue((MPQueueID)gPostID,&p1,&p2,&p3,kDurationImmediate);
  209.             if (kMPTimeoutErr != anErr)break;
  210.         }
  211.     }
  212.     nowTime = UpTime();
  213.  
  214.     uSec = HowLong(nowTime,bgnTime) / ((float)count /2.0);    //Two trips.
  215.     printf("Intertask signalling using queues overhead:%.3f usec \n",uSec);
  216.  
  217.     MPTerminateTask(task,123);
  218.  
  219.     // Time intertask communication.
  220.     printf("\n Semaphores \n");
  221.     gReflectOP = aSemaphore;
  222.     MPCreateSemaphore(1,0,(MPSemaphoreID*)&gWaiterID);
  223.     MPCreateSemaphore(1,0,(MPSemaphoreID*)&gPostID);
  224.  
  225.     bgnTime = UpTime();
  226.     anErr = MPCreateTask(ReflectorTask,NULL,0,NULL,0,0,kNilOptions,&task);
  227.     nowTime = UpTime();
  228.  
  229.     uSec = HowLong(nowTime,bgnTime);
  230.     printf("MPCreateTask overhead:%.3f usec (may vary significantly)\n",uSec);
  231.     if (noErr != anErr)
  232.     {
  233.         printf("Task not created!\n");
  234.         return;
  235.     }
  236.  
  237.     count = 100000;
  238.     bgnTime = UpTime();
  239.     for (i= 0;i<count;i++)
  240.     {
  241.         MPSignalSemaphore((MPSemaphoreID)gWaiterID);
  242.         while (true)
  243.         {
  244.             anErr = MPWaitOnSemaphore((MPSemaphoreID)gPostID,kDurationImmediate);
  245.             if (kMPTimeoutErr != anErr) break;
  246.         }
  247.     }
  248.     nowTime = UpTime();
  249.  
  250.     uSec = HowLong(nowTime,bgnTime);
  251.     uSec /= ((float)count /2.0);//Two trips.
  252.     printf("Intertask signalling using sempahores overhead:%.3f usec \n",uSec);
  253.  
  254.     MPTerminateTask(task,123);
  255.  
  256.     // Time intertask communication.
  257.     printf("\n Event Groups \n");
  258.     gReflectOP = anEvent;
  259.     MPCreateEvent((MPEventID*)&gWaiterID);
  260.     MPCreateEvent((MPEventID*)&gPostID);
  261.  
  262.     bgnTime = UpTime();
  263.     anErr = MPCreateTask(ReflectorTask,NULL,0,NULL,0,0,kNilOptions,&task);
  264.     nowTime = UpTime();
  265.     uSec = HowLong(nowTime,bgnTime);
  266.  
  267.     printf("MPCreateTask overhead:%.3f usec (may vary significantly)\n",uSec);
  268.     if (noErr != anErr)
  269.     {
  270.         printf("Task not created!\n");
  271.         return;
  272.     }
  273.  
  274.     count = 100000;
  275.     bgnTime = UpTime();
  276.     for (i= 0;i<count;i++)
  277.     {
  278.         MPSetEvent((MPEventID)gWaiterID,0x01);
  279.         while (true)
  280.         {
  281.             anErr = MPWaitForEvent((MPEventID)gPostID,&events,kDurationImmediate);
  282.             if (kMPTimeoutErr != anErr)break;
  283.         }
  284.     }
  285.     nowTime = UpTime();
  286.  
  287.     uSec = HowLong(nowTime,bgnTime) / ((float)count /2.0); //Two trips.
  288.     printf("Intertask signalling using events overhead:%.3f usec \n",uSec);
  289.  
  290.     printf("\nPress command-Q to quit.");
  291. }